<template>
  <div class="app">
    <el-container>
      <el-aside
        class="app-side app-side-left"
        :class="isCollapse ? 'app-side-collapsed' : 'app-side-expanded'"
      >
        <div class="app-side-logo">
          <el-avatar
            src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
          ></el-avatar>
        </div>
        <div>
          <!-- 我是样例菜单 -->
          <el-menu
            :router="true"
            default-active="1"
            :collapse="isCollapse"
            class="el-menu-vertical-demo"
            :default-openeds="['1']"
            
          >
            <el-submenu index="1">
              <template slot="title">
                <i class="el-icon-message"></i> 菜单
              </template>
              <el-menu-item index="/home">
                <i class="el-icon-star-on"></i>
                <span slot="title">首页</span>
              </el-menu-item>
            </el-submenu>
            <el-submenu index="2" :collapse="isCollapse">
              <template slot="title">
                <i class="el-icon-menu"></i>书籍菜单
              </template>
              <el-menu-item index="/article">
                <i class="el-icon-notebook-1"></i>
                <span slot="title">书籍管理</span>
              </el-menu-item>
              <el-menu-item index="/publish">
                <i class="el-icon-edit-outline"></i>
                <span slot="title">发布文章</span>
              </el-menu-item>
              <el-menu-item index="/tag">
                <i class="el-icon-postcard"></i>
                <span slot="title">标签管理</span>
              </el-menu-item>
              <el-menu-item index="/category">
                <i class="el-icon-s-grid"></i>分类管理
              </el-menu-item>
              <el-menu-item index="/comment">
                <i class="el-icon-chat-dot-square"></i>评论管理
              </el-menu-item>
            </el-submenu>
            <el-submenu index="3" :collapse="isCollapse">
              <template slot="title">
                <i class="el-icon-user-solid"></i>用户
              </template>
              <el-menu-item index="/admin">
                <i class="el-icon-user"></i>管理员
              </el-menu-item>
              <el-menu-item index="/user">
                <i class="el-icon-user"></i>用户管理
              </el-menu-item>
              <el-menu-item index="/role">
                <i class="el-icon-s-custom"></i>角色管理
              </el-menu-item>
              <el-menu-item index="/permission">
                <i class="el-icon-question"></i>权限管理
              </el-menu-item>
            </el-submenu>
          
          </el-menu>
        </div>
      </el-aside>

      <!-- 头部 -->
      <el-container>
        <el-header class="app-header">
          <div
            style="width: 60px; cursor: pointer;"
            @click.prevent="toggleSideBar"
          >
            <i v-show="!isCollapse" class="el-icon-d-arrow-left"></i>
            <i v-show="isCollapse" class="el-icon-d-arrow-right"></i>
          </div>
          <!-- 我是样例菜单 -->

          <dv-decoration-6 style="width:100%;height:30px;margin-right:20px" />

          <div style="margin-left:20px" class="app-header-userinfo">
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link" style="font-weight:bold">
                {{ user.name }}
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="home" icon="el-icon-edit"
                  >个人主页</el-dropdown-item
                >
                <el-dropdown-item command="modifyPassword" icon="el-icon-edit"
                  >修改密码</el-dropdown-item
                >
                <el-dropdown-item command="logout" icon="el-icon-circle-plus"
                  >退出系统</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
            <!-- 修改密码 -->
            <el-dialog
              title="修改密码"
              :visible.sync="dialogFormVisible"
              width="400px"
            >
              <el-form
                :model="ruleForm"
                status-icon
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                style="width:300px"
              >
                <el-form-item label="原密码" prop="oldPass">
                  <el-input
                    type="password"
                    v-model="ruleForm.oldPass"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="pass">
                  <el-input
                    type="password"
                    v-model="ruleForm.pass"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                  <el-input
                    type="password"
                    v-model="ruleForm.checkPass"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="submitForm()"
                    >提交</el-button
                  >
                  <el-button @click="$refs['ruleForm'].resetFields()"
                    >重置</el-button
                  >
                </el-form-item>
              </el-form>
            </el-dialog>
          </div>
        </el-header>
        <!-- main -->
        <el-main class="app-body">
          <template>
            <app-link v-show="$route.path !== '/home'"></app-link>

            <router-view />
          </template>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { logout } from "@/api/login";
import passwordApi from "@/api/password";
import AppLink from "./AppMain/link.vue";
export default {
  components: { AppLink },
  name: "Container",

  data() {
    //在return声明自定义校验
    const validateOldPass = (rule, value, callback) => {
      passwordApi.checkPwd(this.user.id, value).then(resp => {
        if (resp.data.flag) {
          //校验通过
          callback();
        } else {
          callback(new Error(resp.data.message));
        }
      });
    };
    //校验确认密码
    const validateCheckPass = (rule, value, callback) => {
      if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        oldPass: "",
        pass: "",
        checkPass: ""
      },
      rules: {
        oldPass: [
          { required: true, message: "原密码不能为空", trigger: "blur" },
          { validator: validateOldPass, trigger: "blur" }
        ],
        pass: [{ required: true, message: "密码不能为空", trigger: "blur" }],
        checkPass: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { validator: validateCheckPass, trigger: "change" }
        ]
      },
      dialogFormVisible: false,
      user: this.$store.state.user.user,
      isCollapse: false
    };
  },
  methods: {
    //修改密码
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          passwordApi
            .updatePwd(this.user.id, this.ruleForm.checkPass)
            .then(resp => {
              this.$message({
                message: resp.data.message,
                type: resp.data.flag ? "success" : "warning"
              });
              if (resp.data.flag) {
                //修改成功
                this.logout();
                this.dialogFormVisible = false;
              }
            });
        } else {
          return false;
        }
      });
    },
    handleCommand(command) {
      switch (command) {
        case "home":
          this.$router.push("/homePage");
          break;
        case "modifyPassword":
          this.handleModifyPwd();
          break;
        case "logout":
          this.logout();
          break;
        default:
          return;
      }
    },
    //打开修改密码
    handleModifyPwd() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["ruleForm"].resetFields();
      });
    },
    toggleSideBar() {
      this.isCollapse = !this.isCollapse;
    },
    logout() {
      this.$store
        .dispatch("Logout")
        .then(resp => {
          if (resp.flag) {
            this.$router.push("/login");
          } else {
            this.$message({
              type: "warning",
              message: resp.message,
              duration: 500
            });
          }
        })
        .catch(error => {
          this.$message({
            type: "warning",
            message: "退出错误",
            duration: 500
          });
        });
    },
    /*handleOpen(key, keyPath) {
      // console.log(key, keyPath);
    }*/
  }
};
</script>

<style></style>
